#include <iostream>
#include <cstdlib>
#include "Thread.h"
#include "Lock.h"
#include "Condition.h"
#include "ScopedLock.h"

Thread::Thread() {}

void Thread::start() {
  pthread_create(&mThread, NULL, &startThread, (void*)this);
}

void Thread::stop() {
  pthread_cancel(mThread);
}

void Thread::sleep(int seconds) {
  Lock lock;
  Condition condition;
  std::cout<<"hello";
  {
    ScopedLock scopedLock(&lock);
    condition.timedWait(&lock, seconds);
  }
}

void Thread::join(Thread *thread) {
  if (pthread_join(thread->mThread, NULL)) {
    std::cout << "ERROR: cannot join thread." << std::endl;
    exit(1);
  }
}

void* Thread::startThread(void *thread) {
  Thread *t = static_cast<Thread*>(thread);
  t->run();
  return static_cast<void*>(0);
}
